/***************************************************************************
 创建者: 华磊
 开始时间: 2019.11.23
 copyright: (C) 华友高科
 ***************************************************************************/
#include "joystickview.h"
#include "ui_joystickview.h"
#include <QTimer>
#include <QString>



#include <QDebug>

JoyStickView::JoyStickView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::JoyStickView)
{
    ui->setupUi(this);
    isJogOnStatus=false;
    move(666,0);
//    timer = new QTimer(this);
//    connect(timer, SIGNAL(timeout()), this, SLOT(update()));

}

JoyStickView::~JoyStickView()
{
    delete ui;
}
void JoyStickView::update()
{

//    if (joystick_read_ready(fd))
//    {
//        rc = joystick_read_one_event(fd, &jse);
//        if (rc > 0) {
//            if ((jse.type & JS_EVENT_INIT) == JS_EVENT_INIT) {
//                if ((jse.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
//                    if (jse.value) {
//                        buttons_state |= (1 << jse.number);
//                    }
//                    else {
//                        buttons_state &= ~(1 << jse.number);
//                    }
//                }
//                else if ((jse.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS) {
//                    if (tp_axes) {
//                        if ((jse.number & 1) == 0) {
//                            tp_axes[jse.number / 2].x = jse.value;
//                        }
//                        else {
//                            tp_axes[jse.number / 2].y = jse.value;
//                        }
//                    }

//                }
//            }
//            else {
//                op_times++;
//                if (print_init_stat == 0) {
//                    for (i = 0; i < number_of_btns; i++) {
//                        LOG_DBG("joystick init state: button %d is %s.\n", i, ((buttons_state & (1 << i)) == (1 << i)) ? "DOWN" : "UP");
//                    }

//                    if (tp_axes)
//                        for (i = 0; i < number_of_axes; i++) {
//                            LOG_DBG("joystick init state: axes %d is x=%d  y=%d.\n", i, tp_axes[i].x, tp_axes[i].y);
//                        }
//                    print_init_stat = 1;
//                }

//                if (jse.type  == JS_EVENT_BUTTON)
//                {
//                    if (jse.value) {
//                        buttons_state |= (1 << jse.number);
//                    }
//                    else {
//                        buttons_state &= ~(1 << jse.number);
//                    }

//                    //LOG_DBG("joystick state: button %d is %s.\n", jse.number, ((buttons_state & (1 << jse.number)) == (1 << jse.number)) ? "DOWN" : "UP");
//                    qDebug() << QString("joystick state: button %1 is %2").arg(jse.number).arg(((buttons_state & (1 << jse.number)) == (1 << jse.number)) ? "DOWN" : "UP");

//                    QString contentStr = QString("joystick state: button %1 is %2").arg(jse.number).arg(((buttons_state & (1 << jse.number)) == (1 << jse.number)) ? "DOWN" : "UP");
//                    QString btnIndex = QString("%1").arg(jse.number);
//                    QString btnState = QString("%1").arg(((buttons_state & (1 << jse.number)) == (1 << jse.number)) ? "DOWN" : "UP");

//                    this->setButtonState(contentStr,btnIndex,btnState);
//                }
//                else if (jse.type == JS_EVENT_AXIS)
//                {
//                    if (tp_axes) {
//                        if ((jse.number & 1) == 0) {
//                            tp_axes[jse.number / 2].x = jse.value;
//                        }
//                        else {
//                            tp_axes[jse.number / 2].y = jse.value;
//                        }
//                        //LOG_DBG("joystick state: axes %d is x=%d  y=%d.\n", jse.number / 2, tp_axes[jse.number / 2].x, tp_axes[jse.number / 2].y);
//                        qDebug()<< QString("joystick state: axes %1 is x=%2  y=%3").arg(jse.number / 2).arg(tp_axes[jse.number / 2].x).arg(tp_axes[jse.number / 2].y);

//                        QString content = QString("joystick state: axes %1 is x=%2  y=%3").arg(jse.number / 2).arg(tp_axes[jse.number / 2].x).arg(tp_axes[jse.number / 2].y);
//                        QString axesAaaa = QString("%1").arg(jse.number / 2);
//                        QString axesXvalue = QString("%2").arg(tp_axes[jse.number / 2].x);
//                        QString axesYvalue = QString("%3").arg(tp_axes[jse.number / 2].y);

//                        this->setAxesValues(content,axesAaaa,axesXvalue,axesYvalue);
//                    }
//                    else
//                    {
//                        //LOG_DBG("joystick state: axes %d is %s=%d.\n", jse.number / 2, ((jse.number & 1) == 0) ? "x" : "y", jse.value);
//                    qDebug()<< QString("joystick state: axes %1 is %s=%2").arg(jse.number / 2).arg(((jse.number & 1) == 0) ? "x" : "y", jse.value);

//                    }
//                }
//            }

//        }
//    }

}

void JoyStickView::setAxesValues(QString contStr,QString index_axes,QString coordinates_x,QString coordinates_y)
{
    ui->lineEdit->setText(contStr);

    if(index_axes == "2")
    {
        ui->lineEdit_axis2_x->setText(coordinates_x);
        ui->lineEdit_axis2_y->setText(coordinates_y);

        if(coordinates_x == "-32767" && coordinates_y == "0"){
            ui->button_1P->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1P_pressedSignal();

//            qDebug()<<"发送";
        }
        else if(coordinates_x == "32767" && coordinates_y == "0"){
            ui->button_1N->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1N_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "-32767"){
            ui->button_2P->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_2P_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "32767"){
            ui->button_2N->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_2N_pressedSignal();
        }
        else{
            isJogOnStatus=false;
            ui->button_1P->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_1N->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_2P->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_2N->setStyleSheet("background-color: rgb(255, 255, 255);");
//            emit button_releasedSignal();
        }
    }


    if(index_axes == "0")
    {
        ui->lineEdit_axis0_x->setText(coordinates_x);
        ui->lineEdit_axis0_y->setText(coordinates_y);
        if(coordinates_x == "-32767" && coordinates_y == "0"){
            ui->button_axis0_left->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1P_pressedSignal();

//            qDebug()<<"发送";
        }
        else if(coordinates_x == "32767" && coordinates_y == "0")
        {
            ui->button_axis0_right->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1N_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "-32767")
        {
            ui->button_3P->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_3P_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "32767")
        {
            ui->button_3N->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_3N_pressedSignal();
        }
        else
        {
            ui->button_axis0_left->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_axis0_right->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_3P->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_3N->setStyleSheet("background-color: rgb(255, 255, 255);");
            isJogOnStatus=false;
//            emit button_releasedSignal();
        }
    }


    if(index_axes == "1")
    {
        ui->lineEdit_axis1_x->setText(coordinates_x);
        ui->lineEdit_axis1_y->setText(coordinates_y);
        if(coordinates_x == "-32767" && coordinates_y == "0"){
            ui->button_axis1_left->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1P_pressedSignal();

//            qDebug()<<"发送";
        }
        else if(coordinates_x == "32767" && coordinates_y == "0"){
            ui->button_axis1_right->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_1N_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "-32767")
        {
            ui->button_5P->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_5P_pressedSignal();
        }
        else if(coordinates_x == "0" && coordinates_y == "32767")
        {
            ui->button_5N->setStyleSheet("background-color: rgb(170, 0, 255);");
            isJogOnStatus=true;
//            emit button_5N_pressedSignal();
        }
        else
        {
            ui->button_axis1_left->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_axis1_right->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_5P->setStyleSheet("background-color: rgb(255, 255, 255);");
            ui->button_5N->setStyleSheet("background-color: rgb(255, 255, 255);");
            isJogOnStatus=false;
//            emit button_releasedSignal();
        }
    }

}

void JoyStickView::setButtonState(QString contStr,QString index,QString state)
{
//    qDebug()<<"JoyStickView button "<<index<<state;

    ui->lineEdit->setText(contStr);

    if(index == "0"){
        if(state == "DOWN"){
          ui->button_clrMsgAndServoOn->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit clearMsgAndServoOn_signal();
        }else{
          ui->button_clrMsgAndServoOn->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "1"){
        if(state == "DOWN"){
          ui->button_speedDown->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit speedDown_signal();
        }else{
          ui->button_speedDown->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "2"){
        if(state == "DOWN"){
          ui->button_newOrder->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit lInstruction();
        }else{
          ui->button_newOrder->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "3"){
        if(state == "DOWN"){
          ui->button_speedUp->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit speedUp_signal();
        }else{
          ui->button_speedUp->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "4"){
        if(state == "DOWN"){
          ui->button_4P->setStyleSheet("background-color: rgb(170, 0, 255);");
          isJogOnStatus=true;
//          emit button_4P_pressedSignal();
        }else{
          ui->button_4P->setStyleSheet("background-color: rgb(255, 255, 255);");
          isJogOnStatus=false;
//          emit button_releasedSignal();
        }
    }

    if(index == "5"){
        if(state == "DOWN"){
          ui->button_4N->setStyleSheet("background-color: rgb(170, 0, 255);");
          isJogOnStatus=true;
//          emit button_4N_pressedSignal();
        }else{
          ui->button_4N->setStyleSheet("background-color: rgb(255, 255, 255);");
          isJogOnStatus=false;
//          emit button_releasedSignal();
        }
    }

    if(index == "6"){
        if(state == "DOWN"){
          ui->button_6P->setStyleSheet("background-color: rgb(170, 0, 255);");
          isJogOnStatus=true;
//          emit button_6P_pressedSignal();
        }else{
          ui->button_6P->setStyleSheet("background-color: rgb(255, 255, 255);");
          isJogOnStatus=false;
//          emit button_releasedSignal();

        }
    }

    if(index == "7"){
        if(state == "DOWN"){
          ui->button_6N->setStyleSheet("background-color: rgb(170, 0, 255);");
          isJogOnStatus=true;
//          emit button_6N_pressedSignal();
        }else{
          ui->button_6N->setStyleSheet("background-color: rgb(255, 255, 255);");
          isJogOnStatus=false;
//          emit button_releasedSignal();
        }
    }

    if(index == "8"){
        if(state == "DOWN"){
          ui->button_setJointCoordinate->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit setJointCoordinate_signal();
        }else{
          ui->button_setJointCoordinate->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "9")
    {
        if(state == "DOWN")
        {
          ui->button_startOrStop->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit startOrStop_signal();
        }
        else
        {
          ui->button_startOrStop->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "10")
    {
        if(state == "DOWN")
        {
//          ui->button_reserve->setStyleSheet("background-color: rgb(170, 0, 255);");
        }
        else
        {
//          ui->button_reserve->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }

    if(index == "11")
    {
        if(state == "DOWN")
        {
//          ui->button_modeChange->setStyleSheet("background-color: rgb(170, 0, 255);");
//          emit modeChange_signal();
        }
        else
        {
//          ui->button_modeChange->setStyleSheet("background-color: rgb(255, 255, 255);");
        }
    }
}



bool JoyStickView::isJogOn()
{
    return isJogOnStatus;
}

void JoyStickView::showView_slot(bool isShow)
{
    if(isShow)
    {
        show();
    }
    else
    {
        hide();
    }
}

void JoyStickView::on_button_1P_pressed()
{
    isJogOnStatus=true;
//    emit button_1P_pressedSignal();
    //qDebug()<<"发送";
}

void JoyStickView::on_button_2P_pressed()
{
    isJogOnStatus=true;
//    emit button_2P_pressedSignal();
}

void JoyStickView::on_button_3P_pressed()
{
    isJogOnStatus=true;
//    emit button_3P_pressedSignal();
}

void JoyStickView::on_button_4P_pressed()
{
    isJogOnStatus=true;
//    emit button_4P_pressedSignal();
}

void JoyStickView::on_button_5P_pressed()
{
    isJogOnStatus=true;
//    emit button_5P_pressedSignal();
}

void JoyStickView::on_button_6P_pressed()
{
    isJogOnStatus=true;
//    emit button_6P_pressedSignal();
}

void JoyStickView::on_button_1P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_2P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_3P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_4P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_5P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_6P_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_1N_pressed()
{
    isJogOnStatus=true;
//    emit button_1N_pressedSignal();
}

void JoyStickView::on_button_2N_pressed()
{
    isJogOnStatus=true;
//    emit button_2N_pressedSignal();
}

void JoyStickView::on_button_3N_pressed()
{
    isJogOnStatus=true;
//    emit button_3N_pressedSignal();
}

void JoyStickView::on_button_4N_pressed()
{
    isJogOnStatus=true;
//    emit button_4N_pressedSignal();
}

void JoyStickView::on_button_5N_pressed()
{
    isJogOnStatus=true;
//    emit button_5N_pressedSignal();
}

void JoyStickView::on_button_6N_pressed()
{
    isJogOnStatus=true;
//    emit button_6N_pressedSignal();
}

void JoyStickView::on_button_1N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_2N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_3N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_4N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_5N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_6N_released()
{
    isJogOnStatus=false;
//    emit button_releasedSignal();
}

void JoyStickView::on_button_clrMsgAndServoOn_clicked()
{
//    emit clearMsgAndServoOn_signal();
}

void JoyStickView::on_button_speedUp_clicked()
{
//    emit speedUp_signal();
}

void JoyStickView::on_button_speedDown_clicked()
{
//    emit speedDown_signal();
}

void JoyStickView::on_button_newOrder_clicked()
{
    //emit lInstruction();
}

void JoyStickView::on_button_modeChange_clicked()
{
    //emit modeChange_signal();
}

void JoyStickView::on_button_setJointCoordinate_clicked()
{
    //emit setJointCoordinate_signal();
}

void JoyStickView::on_button_startOrStop_clicked()
{
    //emit startOrStop_signal();
}
